//
//  ModetailWaterFlow.swift
//  CoolImageFound
//
//  Created by  iBrother 李彦兵 on 2018/6/11.
//  Copyright © 2018  iBrother 李彦兵. All rights reserved.
//

import UIKit

class ModetailWaterFlow: UICollectionViewLayout {
    
    /// 列数（可随意更改）
    let columnCount = 2
    /// 每列之间的距离
    let columnMargin: CGFloat = 10
    /// 每行之间的距离
    let rowMargin: CGFloat = 10
    /// 边缘间距
    let edgeInsets = UIEdgeInsetsMake(10, 10, 10, 10)
    
    /// 存放所有cell的布局属性
    var attributes = [UICollectionViewLayoutAttributes]()
    
    /// 存放所有列的当前的高度
    var columHeights = [CGFloat]()
    
    /// 布局初始
    override func prepare() {
        debugPrint(#function)
        super.prepare()
        // 清除之前的计算
        attributes.removeAll()
        columHeights.removeAll()
        for _ in 0..<columnCount {
            columHeights.append(0)
        }
        
        // 创建每一个cell对应的布局属性
        let count = collectionView?.numberOfItems(inSection: 0) ?? 0
        for i in 0..<count {
            // 创建位置
            let indexPath = NSIndexPath(item: i, section: 0)
            // 获取indexPath位置cell对应的布局属性
            let attrs = layoutAttributesForItem(at: indexPath as IndexPath)
            attributes.append(attrs!)
        }
    }
    
    /// UICollectionViewLayout 当继承UICollectionViewLayout时，会不停的调用layoutAttributesForElements方法
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        
        debugPrint(#function)
        return attributes
    }
    
    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        debugPrint(#function)
        // 布局属性
        let attrs = UICollectionViewLayoutAttributes(forCellWith: indexPath)
        
        let collectionViewW = collectionView!.frame.width
        // 设置布局属性的frame
        let w: CGFloat = (collectionViewW - edgeInsets.left - edgeInsets.right - CGFloat(columnCount - 1) * columnMargin) / CGFloat(columnCount)
        
        let h: CGFloat = CGFloat(50 + arc4random_uniform(100))
        
        // 找出最短的那一列
        // 最短的列数
        var destColumn = 0
        // 最短的高度
        var minHeight = columHeights[0]
        // 便利数组找出最短的高度
        for i in 1..<columnCount {
            let height = columHeights[i]
            if minHeight > height {
                minHeight = height
                destColumn = i
            }
        }
        let x: CGFloat = edgeInsets.left + (CGFloat(destColumn) * (w + columnMargin))
        let y = minHeight + columnMargin
        attrs.frame = CGRect(x: x, y: y, width: w, height: h)
        // 更新columHeights
        columHeights[destColumn] = attrs.frame.maxY
        return attrs
        
    }
    /// collectionViewContentSize 返回collectionView的ContentSize
    override var collectionViewContentSize: CGSize {
        debugPrint(#function)
        // 最短的高度
        var maxHeight = columHeights[0]
        // 便利数组找出最短的高度
        for i in 1..<columnCount {
            let height = columHeights[i]
            if maxHeight < height {
                maxHeight = height
            }
        }
        return CGSize(width: 0, height: maxHeight + columnMargin)
    }
}
